/**
 * Copyright (c) 2017-2018, zengjintao (1913188966@qq.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fast.framework.web.orm;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.fast.framework.web.orm.config.DataSourceConfig;
import com.fast.framework.web.orm.config.DataSourceManager;

/**
 * 默认Druid数据源 
 * @author zengjintao
 * @version 1.0
 * @createTime 2017年11月16日下午5:42:35
 */
public class DefaultDataSource implements DataSourceFactory{

	private DataSourceConfig dataSourceConfig;
	private String url;
	private String username;
	private String password;
	private String driverClass = null;	// 由 "com.mysql.jdbc.Driver" 改为 null 让 druid 自动探测 driverClass 值
	
	// 初始连接池大小、最小空闲连接数、最大活跃连接数
	private int initialSize = 10;
	private int minIdle = 10;
	private int maxActive = 100;
	private String configName;
	private DruidDataSource dataSource;
	
	private String validationQuery = "select 1";
	
	@Override
	public DataSource getDataSource() {
		return dataSource;
	}
	
	@Override
	public String getConfigName() {
		return configName;
	}

	public DefaultDataSource(DataSourceConfig dataSourceConfig){
		this.configName = dataSourceConfig.getName() == null ? DataSourceManager.DEFAULT_DATASOURCE : dataSourceConfig.getName();
		this.init(dataSourceConfig);
	}
	
	public DataSourceConfig getDataSourceConfig() {
		return dataSourceConfig;
	}

	public void setDataSourceConfig(DataSourceConfig dataSourceConfig) {
		this.dataSourceConfig = dataSourceConfig;
	}
	
	private void init(DataSourceConfig dataSourceConfig) {
		this.dataSourceConfig = dataSourceConfig;
		this.validationQuery = autoCheckValidationQuery(dataSourceConfig.getUrl());
		dataSource = new DruidDataSource();
		dataSource.setUrl(dataSourceConfig.getUrl());
		dataSource.setPassword(dataSourceConfig.getPassword());
		dataSource.setUsername(dataSourceConfig.getUsername());
		dataSource.setMaxActive(maxActive);
		dataSource.setMinIdle(minIdle);
		dataSource.setInitialSize(initialSize);
		if(driverClass != null)
			dataSource.setDriverClassName(driverClass);
	}

	public String getValidationQuery() {
		return validationQuery;
	}

	/**
	 * @Title: autoCheckValidationQuery  
	 * @Description: 自动设定探测sql 
	 * @param url
	 * @return 
	 * @since V1.0.0
	 */
	private static String  autoCheckValidationQuery(String url){
		if(url.startsWith("jdbc:oracle")){
			return "select 1 from dual";
		}else if(url.startsWith("jdbc:db2")){
			return "select 1 from sysibm.sysdummy1";
		}else if(url.startsWith("jdbc:hsqldb")){
			return "select 1 from INFORMATION_SCHEMA.SYSTEM_USERS";
		}else if(url.startsWith("jdbc:derby")){
			return "select 1 from INFORMATION_SCHEMA.SYSTEM_USERS";
		}
		return "select 1";
	}
	
	
	public String getUrl() {
		return url;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public int getInitialSize() {
		return initialSize;
	}

	public int getMinIdle() {
		return minIdle;
	}

	public int getMaxActive() {
		return maxActive;
	}
}
